package main

import (
	"batch_flyway/internal/config"
	"batch_flyway/internal/handler"
	"flag"
	"log"
	"net/http"
)

var (
	configFile string
)

func main() {
	flag.StringVar(&configFile, "c", "config.yaml", "指定配置文件")
	// 解析命令行参数
	flag.Parse()

	log.Println("Load config file: " + configFile)
	cfg, err := config.LoadConfig(configFile)
	if err != nil || cfg == nil {
		log.Fatalf("Failed to load config: %v", err)
	}

	http.HandleFunc("/query", func(w http.ResponseWriter, r *http.Request) {
		results, err := handler.QueryData(cfg.Databases)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		handler.RenderTemplate(w, results)
	})

	log.Println("Server is running at :8000")
	log.Fatalln(http.ListenAndServe(":8000", nil))
}
